package com.eps.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Utility class for System environment
 *  
 * @author Ali Irawan
 * @version 1.0
 */
public class SystemUtil {
	
	/**
	 * Run shell command 
	 * @param command command
	 * @param args array of arguments
	 * @throws Throwable
	 */
	public static void runCommand(String command, String... args) throws Throwable{
		StringBuilder builder = new StringBuilder();
		builder.append(command).append(' ');
		for(int i=0;i<args.length;i++){
			builder.append(args[i]).append(' ');
		}
		
		try {
			String commandLine = builder.toString();
			System.out.println(commandLine);
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec(commandLine);
			InputStream stderr = proc.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line= null;
			System.out.println("<ERROR>");
            while ( (line = br.readLine()) != null)
                System.out.println(line);
            System.out.println("</ERROR>");
            int exitVal = proc.waitFor();
            System.out.println("Process exitValue: " + exitVal);
		} catch (Throwable e) {
			e.printStackTrace();
			throw e;
		}
	}
	
}